package com.manage.entity;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;

@Entity
@Table(name="t_training_course")
public class TrainingCourse {        //培训课程表实体类
	private Long id;
	private String trainName;
	private Course courses;
	private Date startTime;
	private Date endTime;
	private Staff responsiblePerson;
	
	//业务数据
	private Long coursesId;
	private String coursesName;
	private Long responsiblePersonId;
	private String responsiblePersonName;
	private long count;
	private String examine;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public String getTrainName() {
		return trainName;
	}
	@ManyToOne(cascade=CascadeType.ALL)
	public Course getCourses() {
		return courses;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getStartTime() {
		return startTime;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getEndTime() {
		return endTime;
	}

	@ManyToOne(cascade=CascadeType.ALL)
	public Staff getResponsiblePerson() {
		return responsiblePerson;
	}
	
	@Transient
	public long getCount() {
		return count;
	}
	@Transient
	public Long getCoursesId() {
		return coursesId;
	}
	@Transient
	public String getCoursesName() {
		return coursesName;
	}
	@Transient
	public Long getResponsiblePersonId() {
		return responsiblePersonId;
	}
	@Transient
	public String getResponsiblePersonName() {
		return responsiblePersonName;
	}
	@Transient
	public String getExamine() {
		return examine;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTrainName(String trainName) {
		this.trainName = trainName;
	}

	public void setCourses(Course courses) {
		this.courses = courses;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public void setCount(long count) {
		this.count = count;
	}

	public void setResponsiblePerson(Staff responsiblePerson) {
		this.responsiblePerson = responsiblePerson;
	}
		
	public void setCoursesId(Long coursesId) {
		this.coursesId = coursesId;
	}

	public void setCoursesName(String coursesName) {
		this.coursesName = coursesName;
	}

	public void setResponsiblePersonId(Long responsiblePersonId) {
		this.responsiblePersonId = responsiblePersonId;
	}

	public void setResponsiblePersonName(String responsiblePersonName) {
		this.responsiblePersonName = responsiblePersonName;
	}

	public void setExamine(String examine) {
		this.examine = examine;
	}

	public static Specification<TrainingCourse> getSpecification( final TrainingCourse trainingCourse)
	{
		Specification<TrainingCourse> spec = new Specification<TrainingCourse>() {
			public Predicate toPredicate(Root<TrainingCourse> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != trainingCourse && !StringUtils.isEmpty(trainingCourse.getTrainName())) {
					 Predicate  p1 =  cb.like(root.get("trainName").as(String.class),"%"+ trainingCourse.getTrainName()+ "%");
					 list.add(p1);
				 }
				 if(null != trainingCourse && !StringUtils.isEmpty(trainingCourse.getResponsiblePerson())) {
					 Predicate  p2 =  cb.equal(root.get("responsiblePerson").as(Staff.class),trainingCourse.getResponsiblePerson());
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;	
	}

}
